﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CLibArtreve.bll;
using CLibArtreve.models;
using CLibArtreve.security;

namespace WebAppArtreve.views.shared.components.arts
{
    public partial class WUCArtboard : System.Web.UI.UserControl
    {
        private string _connectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
            }
        }

        private Int64? _userId
        {
            get
            {
                ArtreveIdentity identity = (ArtreveIdentity)Context.User.Identity;
                return identity.User.Id;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //BUTTON (ADD ART) CLICKED
        protected void linkRemoveArtFromArtboard_Command(object sender, CommandEventArgs e)
        {
            removeArt(e.CommandArgument.ToString());
        }

        //REMOVE ART
        private void removeArt(string artId)
        {
            try
            {
                //1. BLL
                //-----------------------
                UserArtBLL bllUserArt = new UserArtBLL(_connectionString);

                //2. MODELS
                //----------------------- 
                UserArt userArt = new UserArt();

                //3. INSERT ART IN MODEL
                //-----------------------                

                userArt.Id = _userId;
                userArt.ArtId = Convert.ToInt64(artId);

                //4. DELETE ART FROM ARTBOARD IN DB
                //-----------------------   
                bllUserArt.DeleteUserArt(userArt);

                //REFRESH PAGE
                Response.AppendHeader("Refresh", "0;URL=Artboard.aspx");

            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        protected void ODSArtboardArtsALL_OnObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            //STEP 2. CREATE BLL
            ArtBLL ObjectInstance = new ArtBLL(_connectionString);
            //STEP 3. CONNECT TYPENAME
            e.ObjectInstance = ObjectInstance;
        }

        protected void ODSArtboardArtsALL_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["id"] = _userId;
        }
    }
}